package com.yhk.apiinterface.controller;

import com.yhk.apiclientsdk.model.User;
import com.yhk.apiclientsdk.utils.SignUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @version 1.0
 * @Author moresuo
 * @Date 2024/3/12 21:57
 * @注释 查询名称接口
 *
 */
@RestController
@RequestMapping("/name")
public class NameController {
    @GetMapping("/")
    public String getNameByGet(String name, HttpServletRequest request){

        return "GET 你的名字是"+name;
    }
    @PostMapping("/")
    public String getNameByPost(@RequestParam String name){
        return "POST 你的名字是"+name;
    }
    @PostMapping("/user")
    public String getUserNameByPost(@RequestBody User user, HttpServletRequest request){
        String accessKey = request.getHeader("accessKey");
        String nonce = request.getHeader("nonce");
        String body = request.getHeader("body");
        String timestamp = request.getHeader("timestamp");
        String sign = request.getHeader("sign");

        if (!"moresuo".equals(accessKey)) {
            throw new RuntimeException("参数错误");
        }
        if(Long.parseLong(nonce)>10000){
            throw new RuntimeException("无权限");
        }
        String serverSign = SignUtils.genSign(body, "moresuo");
        if(!sign.equals(serverSign)){
            throw new RuntimeException("无权限");
        }
        return "POST 你的名字是"+user.getUserName();
    }
}
